import BattleStruct from "../Structs/BattleStruct";
import C from "../Configs/Config";
import G from "../Utilities/Global";

/**
 * 关卡选择界面
 * created by Five on 2018-12-21 12:09:39
 */
const { ccclass, property } = cc._decorator

@ccclass
export default class UIChapter extends cc.Component {
    // 章节背景
    @property(cc.Sprite)
    backAsset: cc.Sprite = null;

    @property(cc.Node)
    touchPrevent: cc.Node = null;

    // 章节挂载点
    @property(cc.Node)
    chapterAttach: cc.Node = null;

    @property(cc.Node)
    gateAttach: cc.Node = null;

    // 章节按钮 
    chapters: Map<number, cc.Node>;

    // 关卡按钮
    gates: Map<number, cc.Node>;

    start() {
        this.chapters = new Map<number, cc.Node>();
        this.gates = new Map<number, cc.Node>();

        this.touchPrevent.on(cc.Node.EventType.TOUCH_START, (e: Event) => {
            e.stopPropagation();
        });

        this.Init();
    }

    Init() {
        let chaptersNode = this.chapterAttach.getChildByName("chapters");
        for (let i = 1; i <= 5; i++) {
            this.chapters.set(i, chaptersNode.getChildByName(i + ""));
        }
        this.chapters.forEach((v, k) => {
            v.active = k == this.current;
            v.on(cc.Node.EventType.TOUCH_START, () => { cc.log('try ' + k); });
        });

        for (let i = 1; i <= 5; i++) {
            this.gates.set(i, this.gateAttach.getChildByName(i + ""));
        }
        this.selectChapter(this.current);
    }

    close() {
        G().Sound.playSFX("btn_clicked");
        this.node.removeFromParent();
    }

    async selectChapter(index: number) {
        let affix = "chapter/";
        this.backAsset.spriteFrame = await G().Res.GetSpriteFrame(affix + "chapter_" + index) as cc.SpriteFrame;

        this.gates.forEach(async (v, k) => {
            let back = v.getChildByName("back").getComponent(cc.Sprite);
            let front = v.getChildByName("front").getComponent(cc.Sprite);
            let label = v.getChildByName("name").getComponent(cc.Label);
            back.spriteFrame = await G().Res.GetSpriteFrame(affix + "battle_bg_" + index) as cc.SpriteFrame;
            front.spriteFrame = await G().Res.GetSpriteFrame(affix + "battle_icon_" + index + "_" + k) as cc.SpriteFrame;
            let stage = (index - 1) * 5 + k - 1;
            let mapName = C().Map.GetNameByIndex(stage);
            label.string = mapName;
            front.node.off(cc.Node.EventType.TOUCH_START);
            front.node.on(cc.Node.EventType.TOUCH_START, () => {
                G().SelectedMap = stage;
                G().Sound.playSFX("btn_clicked");
                this.node.dispatchEvent(new cc.Event("EnterBattle", true));
            });
        });
    }

    current: number = 1;
    leftClick() {
        this.current--;
        if (this.current < 1) this.current = 5;
        this.chapters.forEach((v, k) => {
            v.active = k == this.current;
            v.on(cc.Node.EventType.TOUCH_START, () => { cc.log('try ' + k); });
        });
        this.selectChapter(this.current);
        G().Sound.playSFX("btn_clicked");
    }

    rightClick() {
        this.current++;
        if (this.current > 5) this.current = 1;
        this.chapters.forEach((v, k) => {
            v.active = k == this.current;
            v.on(cc.Node.EventType.TOUCH_START, () => { cc.log('try ' + k); });
        });
        this.selectChapter(this.current);
        G().Sound.playSFX("btn_clicked");
    }
}